//
//  YXMainViewController.swift
//  SinaWeiBo
//
//  Created by Vergil on 2017/4/5.
//  Copyright © 2017年 Vergil. All rights reserved.
//

import UIKit

class YXMainViewController: UIViewController {

    //访客视图字典
    var visitViewInfo:[String: String]?
    ///是否正在加载
    var isLoadMore:Bool?
    ///tableview
    var tableView:UITableView?
    ///刷新控件
    var refreshC:UIRefreshControl?
    
    lazy var navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
    lazy var navItem = UINavigationItem()

    override func viewDidLoad() {
        super.viewDidLoad()

        isLoadMore = false
        setupUI()
        YXNetWork.Manager.userLogin ? loadData() : ()
        NotificationCenter.default.addObserver(self, selector: #selector(successLogin), name: NSNotification.Name(rawValue: WBUserSuccessLoginNotification), object: nil)
        
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    //重写title的set方法
    override var title: String? {
        didSet{
            navItem.title = title
        }
    }
    
    func loadData() {
        //如果子类不实现该方法,默认结束刷新
        refreshC?.endRefreshing()
    }
    
    //成功登录返回
    @objc fileprivate func successLogin() {
        
        //重新加载导航栏
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        //访问view的getter方法时,如果view == nil,会重新执行loadview -> viewdidload
        view = nil
        
        NotificationCenter.default.removeObserver(self)
        
    }

}

// MARK: - 访客视图按钮监听方法
extension YXMainViewController {
    
    @objc fileprivate func loginBtnClick() {
        print("点击登录")
        //发送登录通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserLoginNotification), object: nil)
        
    }
    
    @objc fileprivate func regiserBtnClick() {
        print("点击注册")
    }
    
}

// MARK: - 设置ui
extension YXMainViewController {
    
    fileprivate func setupUI() {
        
        view.backgroundColor = UIColor.white
        
        //自定义导航栏
        view.addSubview(navBar)
        navBar.items = [navItem]
        
        //设置导航条背景颜色
//        navBar.barTintColor = UIColor.lightGray
        //设置导航条标题颜色
        navBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.darkGray]
        //设置导航条系统按钮颜色
        navBar.tintColor = UIColor.orange
        
        YXNetWork.Manager.userModel.access_token != nil ? setupTableView() : setupVisitUI()
        
    }
    
    //添加tableview
    func setupTableView() {
        
        //添加tableView
        tableView = UITableView(frame: view.bounds)
        view.insertSubview(tableView!, belowSubview: navBar)
        tableView?.tableFooterView = UIView()
        tableView?.contentInset = UIEdgeInsets(top: 44, left: 0, bottom: 0, right: 0)
        
        tableView?.dataSource = self
        tableView?.delegate = self
        
        //添加下拉刷新
        refreshC = UIRefreshControl()
        refreshC?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        tableView?.addSubview(refreshC!)
        
    }
    
    //添加访客视图
    func setupVisitUI() {
        
        let visitView = YXVisitView(frame: view.bounds)
        visitView.backgroundColor = UIColor.init(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        view.insertSubview(visitView, belowSubview: navBar)
        visitView.visitView = visitViewInfo
        visitView.loginBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        visitView.registBtn.addTarget(self, action: #selector(regiserBtnClick), for: .touchUpInside)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(regiserBtnClick))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginBtnClick))
        
    }
   
}

//MARK: - 代理和数据源
//利用 extension 隔离代码,类似于oc中的category
//extension 中不能重写父类本类中的方法
extension YXMainViewController:UITableViewDataSource,UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    //即将加载完cell的时候
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        //判断是否是最后一行
        //当前第几行
        let row = indexPath.row
        //最后一组组数
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            return
        }
        
        //当前组的行数
        let count = tableView.numberOfRows(inSection: indexPath.section)
        
        if row == count - 1 && !isLoadMore! {
            
            isLoadMore = true
            
            loadData()
            
        }
    }
    
}

